﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AppStore.Domain.Abstract;
using AppStore.Domain.Entities;
using AppStore.Domain.DAO;
using PagedList;

namespace AppStore.Controllers
{ 
    [Authorize(Roles = "Administrator")]
    public class ApplicationManagerController : Controller
    {

        private IApplicationRepository _applicationRepository;
        private ICategoryRepository _categoryRepository;
        private int pageSize = 3;

        public ApplicationManagerController(IApplicationRepository applicationRepository, ICategoryRepository categoryRepository)
        {
            _applicationRepository = applicationRepository;
            _categoryRepository = categoryRepository;
        }

        public ViewResult Index(int? page)
        {
            var apps = _applicationRepository.Applications.OrderBy(a => a.name);
            page = page ?? 1;

            return View(apps.ToPagedList((int)page, pageSize));
        }

        public ViewResult Details(int applicationId)
        {
            Application application = _applicationRepository.Applications.Single(a => a.applicationId == applicationId);
            return View(application);
        }

        public ActionResult Create()
        {
            ViewBag.categoryId = new SelectList(_categoryRepository.Categories, "categoryId", "name");
            return View();
        } 

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Application application, HttpPostedFileBase Image)
        {
            if (ModelState.IsValid)
            {
                if (Image != null)
                {
                    application.ImageMimeType = Image.ContentType;
                    application.ImageData = new byte[Image.ContentLength];
                    Image.InputStream.Read(application.ImageData, 0, Image.ContentLength);
                }
                _applicationRepository.addApplication(application);
                return RedirectToAction("Index");  
            }

            ViewBag.categoryId = new SelectList(_categoryRepository.Categories, "categoryId", "name", application.categoryId);
            return View(application);
        }
        
        //
        // GET: /StoreManager/Edit/5
        
        public ActionResult Edit(int applicationId)
        {
            Application application = _applicationRepository.Applications.Single(a => a.applicationId == applicationId);
            ViewBag.categoryId = new SelectList(_categoryRepository.Categories, "categoryId", "name", application.categoryId);
            return View(application);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Application application, HttpPostedFileBase Image)
        {
            if (ModelState.IsValid)
            {
                if (Image != null)
                {
                    application.ImageMimeType = Image.ContentType;
                    application.ImageData = new byte[Image.ContentLength];
                    Image.InputStream.Read(application.ImageData, 0, Image.ContentLength);
                }
                _applicationRepository.saveEditedChanges(application);
                return RedirectToAction("Index");
            }
            ViewBag.categoryId = new SelectList(_categoryRepository.Categories, "categoryId", "name", application.categoryId);
            return View(application);
        }

        //
        // GET: /StoreManager/Delete/5
 
        public ActionResult Delete(int applicationId)
        {
            Application application = _applicationRepository.Applications.Single(a => a.applicationId == applicationId);
            return View(application);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int applicationId)
        {            
            _applicationRepository.removeApplication(applicationId);
            return RedirectToAction("Index");
        }
    }
}